<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      
      <el-form-item label="一级分类" prop="plusDsiCategoryfirst">
        <el-input
          v-model="queryParams.plusDsiCategoryfirst"
          placeholder="请输入一级分类"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="二级分类" prop="plusDsiCategorysecond">
        <el-input
          v-model="queryParams.plusDsiCategorysecond"
          placeholder="请输入二级分类"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="三级分类" prop="plusDsiCategorythird">
        <el-input
          v-model="queryParams.plusDsiCategorythird"
          placeholder="请输入三级分类"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="病虫害名称" prop="plusDsiName">
        <el-input
          v-model="queryParams.plusDsiName"
          placeholder="请输入病虫害名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类别" prop="disPestType">
        <el-select v-model="queryParams.disPestType" placeholder="请选择类别" style="width: 240px" clearable>
          <el-option
            v-for="dict in peach_knowledge_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="英文名称" prop="plusDsiNameeng">
        <el-input
          v-model="queryParams.plusDsiNameeng"
          placeholder="请输入英文名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="别名" prop="plusDsiSynonyms">
        <el-input
          v-model="queryParams.plusDsiSynonyms"
          placeholder="请输入别名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建人" prop="createdBy">
        <el-input
          v-model="queryParams.createdBy"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新时间" prop="lastupdatedAt">
        <el-date-picker clearable
          v-model="queryParams.lastupdatedAt"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新人" prop="lastupdatedBy">
        <el-input
          v-model="queryParams.lastupdatedBy"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="删除时间" prop="deletedAt">
        <el-date-picker clearable
          v-model="queryParams.deletedAt"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="删除人" prop="deletedBy">
        <el-input
          v-model="queryParams.deletedBy"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否删除" prop="isdeleted">
        <el-input
          v-model="queryParams.isdeleted"
          placeholder="请输入默认0，1为删除"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="时间戳" prop="timestamp">
        <el-date-picker clearable
          v-model="queryParams.timestamp"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="公司ID" prop="companyid">
        <el-input
          v-model="queryParams.companyid"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组织ID" prop="organizeid">
        <el-input
          v-model="queryParams.organizeid"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="省名称" prop="provice">
        <el-input
          v-model="queryParams.provice"
          placeholder="请输入省名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="省编码" prop="proviceCode">
        <el-input
          v-model="queryParams.proviceCode"
          placeholder="请输入省编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="市名称" prop="city">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入市名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="市编码" prop="cityCode">
        <el-input
          v-model="queryParams.cityCode"
          placeholder="请输入市编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="县名称" prop="district">
        <el-input
          v-model="queryParams.district"
          placeholder="请输入县名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="县编码" prop="districtCode">
        <el-input
          v-model="queryParams.districtCode"
          placeholder="请输入县编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['knowledge:knowledge:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['knowledge:knowledge:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['knowledge:knowledge:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['knowledge:knowledge:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="knowledgeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="一级分类" align="center" prop="plusDsiCategoryfirst" />
      <el-table-column label="二级分类" align="center" prop="plusDsiCategorysecond" />
      <el-table-column label="三级分类" align="center" prop="plusDsiCategorythird" />
      <el-table-column label="病虫害名称" align="center" prop="plusDsiName" />
      <el-table-column label="类别" align="center" prop="disPestType">
        <template #default="scope">
          <dict-tag :options="peach_knowledge_type" :value="scope.row.disPestType"/>
        </template>
      </el-table-column>
      <el-table-column label="英文名称" align="center" prop="plusDsiNameeng" />
      <el-table-column label="别名" align="center" prop="plusDsiSynonyms" />
      <el-table-column label="简介" align="center" prop="plusDsiIntroduction" />
      <el-table-column label="图片路径" align="center" prop="plusDsiPath" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.plusDsiPath" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="危害症状" align="center" prop="plusDsiDamagesym" />
      <el-table-column label="病源" align="center" prop="plusDsiPathogen" />
      <el-table-column label="侵染" align="center" prop="plusDsiCycle" />
      <el-table-column label="发生规律" align="center" prop="plusDsiOfactor" />
      <el-table-column label="形态" align="center" prop="plusDsiMorphology" />
      <el-table-column label="习性" align="center" prop="plusDsiHabits" />
      <el-table-column label="防治方法" align="center" prop="plusDsiCmethod" />
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createdBy" />
      <el-table-column label="更新时间" align="center" prop="lastupdatedAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.lastupdatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="lastupdatedBy" />
      <el-table-column label="删除时间" align="center" prop="deletedAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.deletedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="删除人" align="center" prop="deletedBy" />
      <el-table-column label="是否删除" align="center" prop="isdeleted" />
      <el-table-column label="时间戳" align="center" prop="timestamp" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.timestamp, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="公司ID" align="center" prop="companyid" />
      <el-table-column label="组织ID" align="center" prop="organizeid" />
      <el-table-column label="省名称" align="center" prop="provice" />
      <el-table-column label="省编码" align="center" prop="proviceCode" />
      <el-table-column label="市名称" align="center" prop="city" />
      <el-table-column label="市编码" align="center" prop="cityCode" />
      <el-table-column label="县名称" align="center" prop="district" />
      <el-table-column label="县编码" align="center" prop="districtCode" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['knowledge:knowledge:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['knowledge:knowledge:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改病虫害百科对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="knowledgeRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="一级分类" prop="plusDsiCategoryfirst">
          <el-input v-model="form.plusDsiCategoryfirst" placeholder="请输入一级分类" />
        </el-form-item>
        <el-form-item label="二级分类" prop="plusDsiCategorysecond">
          <el-input v-model="form.plusDsiCategorysecond" placeholder="请输入二级分类" />
        </el-form-item>
        <el-form-item label="三级分类" prop="plusDsiCategorythird">
          <el-input v-model="form.plusDsiCategorythird" placeholder="请输入三级分类" />
        </el-form-item>
        <el-form-item label="病虫害名称" prop="plusDsiName">
          <el-input v-model="form.plusDsiName" placeholder="请输入病虫害名称" />
        </el-form-item>
        <el-form-item label="类别" prop="disPestType">
          <el-select v-model="form.disPestType" placeholder="请选择类别">
            <el-option
              v-for="dict in peach_knowledge_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="英文名称" prop="plusDsiNameeng">
          <el-input v-model="form.plusDsiNameeng" placeholder="请输入英文名称" />
        </el-form-item>
        <el-form-item label="别名" prop="plusDsiSynonyms">
          <el-input v-model="form.plusDsiSynonyms" placeholder="请输入别名" />
        </el-form-item>
        <el-form-item label="简介" prop="plusDsiIntroduction">
          <el-input v-model="form.plusDsiIntroduction" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="图片路径" prop="plusDsiPath">
          <image-upload v-model="form.plusDsiPath"/>
        </el-form-item>
        <el-form-item label="危害症状" prop="plusDsiDamagesym">
          <el-input v-model="form.plusDsiDamagesym" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="病源" prop="plusDsiPathogen">
          <el-input v-model="form.plusDsiPathogen" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="侵染" prop="plusDsiCycle">
          <el-input v-model="form.plusDsiCycle" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="发生规律" prop="plusDsiOfactor">
          <el-input v-model="form.plusDsiOfactor" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="形态" prop="plusDsiMorphology">
          <el-input v-model="form.plusDsiMorphology" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="习性" prop="plusDsiHabits">
          <el-input v-model="form.plusDsiHabits" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="防治方法" prop="plusDsiCmethod">
          <el-input v-model="form.plusDsiCmethod" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建人" prop="createdBy">
          <el-input v-model="form.createdBy" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="更新时间" prop="lastupdatedAt">
          <el-date-picker clearable
            v-model="form.lastupdatedAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新人" prop="lastupdatedBy">
          <el-input v-model="form.lastupdatedBy" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="删除时间" prop="deletedAt">
          <el-date-picker clearable
            v-model="form.deletedAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="" prop="deletedBy">
          <el-input v-model="form.deletedBy" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="是否删除" prop="isdeleted">
          <el-input v-model="form.isdeleted" placeholder="请输入默认0，1为删除" />
        </el-form-item>
        <el-form-item label="时间戳" prop="timestamp">
          <el-date-picker clearable
            v-model="form.timestamp"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="公司ID" prop="companyid">
          <el-input v-model="form.companyid" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="组织ID" prop="organizeid">
          <el-input v-model="form.organizeid" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="省名称" prop="provice">
          <el-input v-model="form.provice" placeholder="请输入省名称" />
        </el-form-item>
        <el-form-item label="省编码" prop="proviceCode">
          <el-input v-model="form.proviceCode" placeholder="请输入省编码" />
        </el-form-item>
        <el-form-item label="市名称" prop="city">
          <el-input v-model="form.city" placeholder="请输入市名称" />
        </el-form-item>
        <el-form-item label="市编码" prop="cityCode">
          <el-input v-model="form.cityCode" placeholder="请输入市编码" />
        </el-form-item>
        <el-form-item label="县名称" prop="district">
          <el-input v-model="form.district" placeholder="请输入县名称" />
        </el-form-item>
        <el-form-item label="县编码" prop="districtCode">
          <el-input v-model="form.districtCode" placeholder="请输入县编码" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Knowledge">
import { listKnowledge, getKnowledge, delKnowledge, addKnowledge, updateKnowledge } from "@/api/peach/knowledge";

const { proxy } = getCurrentInstance();
const { peach_knowledge_type } = proxy.useDict('peach_knowledge_type');

const knowledgeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    plusDsiCategoryfirst: null,
    plusDsiCategorysecond: null,
    plusDsiCategorythird: null,
    plusDsiName: null,
    disPestType: null,
    plusDsiNameeng: null,
    plusDsiSynonyms: null,
    plusDsiIntroduction: null,
    plusDsiPath: null,
    plusDsiDamagesym: null,
    plusDsiPathogen: null,
    plusDsiCycle: null,
    plusDsiOfactor: null,
    plusDsiMorphology: null,
    plusDsiHabits: null,
    plusDsiCmethod: null,
    createdAt: null,
    createdBy: null,
    lastupdatedAt: null,
    lastupdatedBy: null,
    deletedAt: null,
    deletedBy: null,
    isdeleted: null,
    timestamp: null,
    companyid: null,
    organizeid: null,
    provice: null,
    proviceCode: null,
    city: null,
    cityCode: null,
    district: null,
    districtCode: null
  },
  rules: {
    plusDsiCategoryfirst: [
      { required: true, message: "一级分类不能为空", trigger: "blur" }
    ],
    plusDsiCategorysecond: [
      { required: true, message: "二级分类不能为空", trigger: "blur" }
    ],
    plusDsiCategorythird: [
      { required: true, message: "三级分类不能为空", trigger: "blur" }
    ],
    plusDsiName: [
      { required: true, message: "病虫害名称不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询病虫害百科列表 */
function getList() {
  loading.value = true;
  listKnowledge(queryParams.value).then(response => {
    knowledgeList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    plusDsiCategoryfirst: null,
    plusDsiCategorysecond: null,
    plusDsiCategorythird: null,
    plusDsiName: null,
    disPestType: null,
    plusDsiNameeng: null,
    plusDsiSynonyms: null,
    plusDsiIntroduction: null,
    plusDsiPath: null,
    plusDsiDamagesym: null,
    plusDsiPathogen: null,
    plusDsiCycle: null,
    plusDsiOfactor: null,
    plusDsiMorphology: null,
    plusDsiHabits: null,
    plusDsiCmethod: null,
    createdAt: null,
    createdBy: null,
    lastupdatedAt: null,
    lastupdatedBy: null,
    deletedAt: null,
    deletedBy: null,
    isdeleted: null,
    timestamp: null,
    companyid: null,
    organizeid: null,
    provice: null,
    proviceCode: null,
    city: null,
    cityCode: null,
    district: null,
    districtCode: null
  };
  proxy.resetForm("knowledgeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加病虫害百科";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getKnowledge(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改病虫害百科";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["knowledgeRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateKnowledge(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addKnowledge(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除病虫害百科编号为"' + _ids + '"的数据项？').then(function() {
    return delKnowledge(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('knowledge/knowledge/export', {
    ...queryParams.value
  }, `knowledge_${new Date().getTime()}.xlsx`)
}

getList();
</script>
